/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.service.ICodingGeneratorService;
import com.ce.pms.common.service.IOrganizationalStructureService;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.house.service.IHouseService;
import com.ce.pms.house.service.IProRightChangeService;
import com.ce.pms.renovation.dao.IRenovateLicenceDao;
import com.ce.pms.renovation.dao.IRenovateRequisitionDao;
import com.ce.pms.renovation.model.RenovateChargeItemDO;
import com.ce.pms.renovation.model.RenovateLicenceDO;
import com.ce.pms.renovation.model.RenovateRequisitionDO;
import com.ce.pms.renovation.service.IRenovateChargeService;
import com.ce.pms.renovation.service.IRenovateManageService;
import com.ce.pms.renovation.vo.RenovateChargeItemVO;
import com.ce.pms.renovation.vo.RenovateRequisitionQueryVO;
import com.ce.pms.renovation.vo.RenovateRequisitionVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：装修手续办理服务类<br>
 * @author 罗军林 创建日期：2008-12-22
 */
public class RenovateManageServiceImpl extends PmsBaseServiceImpl implements IRenovateManageService{
	private IRenovateRequisitionDao  renovateRequisitionDao;
	private IHouseDao houseDao;
	private IHouseService houseService;
	private IProRightChangeService proRightChangeService;
	private IRenovateChargeService renovateChargeService;
	private IRenovateLicenceDao renovateLicenceDao;
	private ICodingGeneratorService codingGeneratorService;
	private IOrganizationalStructureService organizationalStructureService;

   /**
    * 功能描述:<br>
    * @param requisitionVO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-23<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public RenovateRequisitionVO addRequisition(RenovateRequisitionVO requisitionVO){
	   HouseDomain house=houseDao.getHouseDomainByHouseNumber(requisitionVO.getHouseNumber());
	   isAllowAddRequisition(requisitionVO.getHouseNumber());

	   String requistionNumber=codingGeneratorService.generateRequisitionNumber();
	   checkParameterIsNull(requistionNumber, "renovate:renovate_requistion_number_err");

	   RenovateRequisitionDO renovateRequisitionDO=new RenovateRequisitionDO();
	   assembleRequisitionV2D(requisitionVO,renovateRequisitionDO);
	   renovateRequisitionDO.setFeeState(SysrefcodeDO.RM_FEE_STATE_NO);
	   renovateRequisitionDO.setProcessState(SysrefcodeDO.RM_RENOVATE_PROCESS_STATE_ADD);
	   renovateRequisitionDO.setCheckConfirmYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
	   renovateRequisitionDO.setRequisitionState(SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NEW);
	   renovateRequisitionDO.setHouse(house);
	   renovateRequisitionDO.setSysInfo(this.createInfo(requisitionVO.getCreator()));
	   renovateRequisitionDO.setRequisitionNumber(requistionNumber);
	   renovateRequisitionDao.saveObject(renovateRequisitionDO);

	   notifyAddRenovateRequisition(renovateRequisitionDO.getKeyword());

	   return assembleRequisitionD2V(renovateRequisitionDO);
   }

   /**
    * 功能描述:修改装修申请，如果面积做了修改，将修改对应的费用<br>
    * @param requisitionVO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public RenovateRequisitionVO editRequisition(RenovateRequisitionVO requisitionVO){
	   RenovateRequisitionDO renovateRequistionDO= renovateRequisitionDao.findById(requisitionVO.getKeyword());
	   isAllowEditRequisition(renovateRequistionDO);

	   boolean isEditArea = !requisitionVO.getArea().equals(renovateRequistionDO.getArea());

	   assembleRequisitionV2D(requisitionVO,renovateRequistionDO);
	   this.modifyInfo(renovateRequistionDO.getSysInfo(),requisitionVO.getCreator());

	   if( isEditArea ){
		   notifyEditRenovateRequisition(renovateRequistionDO.getKeyword());
	   }
	   return assembleRequisitionD2V(renovateRequistionDO);
   }

   /**
    * 功能描述:删除装修申请<br>
    * @param requisitionId<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public void deleteRequisition(Long requisitionId){
	   checkParameterIsNull(requisitionId, "renovate:renovate_requistion_id_null");
	   RenovateRequisitionDO renovateRequistionDO= renovateRequisitionDao.findById(requisitionId);
	   isAllowDeleteRequisition(requisitionId);
	   renovateChargeService.deleteRenovateChargeItem(renovateRequistionDO);
	   renovateRequisitionDao.deleteObject(renovateRequistionDO);
   }

   /**
    * 功能描述:作废指定的装修申请<br>
    * 1)流程状态改为异常完结 2）审批状态改为空 2)删除未处理的费用记录
    * @param requisitionId<br>
    * Create author:罗军林<br>
    * Create on:2009-1-4<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public void blankOutRequistion(Long requisitionId){
	   checkParameterIsNull(requisitionId, "renovate:renovate_requistion_id_null");
	   RenovateRequisitionDO renovateRequistionDO= renovateRequisitionDao.findById(requisitionId);
	   isAllowBlankRequisition(renovateRequistionDO);
	   renovateChargeService.deleteRenovateChargeRecord(renovateRequistionDO);
	   renovateRequistionDO.setRequisitionState(null);
	   renovateRequistionDO.setProcessState(SysrefcodeDO.RM_RENOVATE_PROCESS_STATE_EXP);
   }


   /**
    * 功能描述:<br>
    * @param queryCondition
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-22<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   @SuppressWarnings("unchecked")
	public Page queryRequisitions(RenovateRequisitionQueryVO queryCondition){
			Page page=renovateRequisitionDao.queryRequisitions(queryCondition);
			List<RenovateRequisitionVO> result=page.getResult();
			for(RenovateRequisitionVO item : result){
				setStateView(item);
			}
		    return page;
	   }

	/**
	 * 功能描述:设置各种状态页面展示的值<br>
	 * @param item<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-24<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void setStateView(RenovateRequisitionVO item) {
		SysrefcodeDO feeState=sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.RM_RENOVATE_FEE_STATE_CODE,item.getFeeState());
		item.setFeeStateView(feeState==null ? null : feeState.getName());

		SysrefcodeDO requistionState=sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_CODE, item.getRequisitionState());
		item.setRequisitionStateView(requistionState==null ? null : requistionState.getName());

		SysrefcodeDO licenceState=sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.RM_RENOVATE_LICENCE_STATE_CODE, item.getRenovateLicenceState());
		item.setRenovateLicenceStateView(licenceState==null ? SysrefcodeDO.RM_RENOVATE_LICENCE_STATE_NO_VIEW : licenceState.getName());

		if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(item.getCheckConfirmYn())){
			item.setCheckConfirmYnView(SysrefcodeDO.IS_NO);
		}else if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(item.getCheckConfirmYn())){
			item.setCheckConfirmYnView(SysrefcodeDO.IS_YES);
		}
	}


	/**
	 * 功能描述:查询指定装修申请的详细信息<br>
	 * @param requisitionId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
   public RenovateRequisitionVO getRequisition(Long requisitionId){
	   RenovateRequisitionDO requisitionDO = renovateRequisitionDao.findById(requisitionId);
	   return assembleRequisitionD2V(requisitionDO);
   }

		/**
		 * 功能描述:查询指定许可证的收费项目列表<br>
		 * @param requisitionId
		 * @return<br>
		 * Create author:罗军林<br>
		 * Create on:2008-12-25<br>
		 * Edit author:<br>
		 * Edit on:<br>
		 * Why:<br>
		 */
	  public List<RenovateChargeItemVO> getRequisitionChargeItemList(Long licenceId){
		  checkParameterIsNull(licenceId, "renovate:renovate_licence_id_not_null");

		  RenovateLicenceDO licence = renovateLicenceDao.findById(licenceId);

		   return assembleRenovateChargeItemList(licence.getRenovateRequisition().getRenovateChargeItemDOSet());
	  }


   /**
    * 功能描述: 是否允许新增，根据业务规则判断,有一项不满足将抛出异常;<br>
    * <ol>
    * <li>房间是否存在</li>
    * <li>房间是否存在产权</li>
    * <li>房间是否处于退伙中</li>
    * <li>房间是否已经编制过申请尚未完结<li>
    * </ol>
    * @param houseNumber
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-23<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public boolean isAllowAddRequisition(String houseNumber){
	   HouseDomain house=houseDao.getHouseDomainByHouseNumber(houseNumber);
	   //房间是否存在
	   if(null==house){
		   throw new BusinessException("house:pro_right_number_not_exist_exception",BusinessException.WARNING);
	   }

	   //房间是否存在产权
	   String ownerName=houseService.getOwnerNameBy(house.getHouseNumber());
	   if(BlankUtil.isBlank( ownerName )){
		   throw new BusinessException("house:pro_right_number_no_owner_exception",BusinessException.WARNING);
	   }

	   //房间是否处于退伙中
	   String code=proRightChangeService.checkTheHouseNumber(house.getKeyword());
	   if(null!=code){
		   throw new BusinessException("renovate:renovate_house_is_exit",BusinessException.WARNING);
	   }

	   //房间是否已经编制过申请尚未完结
	   RenovateRequisitionDO renovateDO= renovateRequisitionDao.getRenovateRequisitionDOByHouseId(house.getKeyword());
	   if(null!=renovateDO){
		   throw new BusinessException("renovate:is_has_requistion",BusinessException.ERROR);
	   }
	   return true;
   }

   /**
    * 功能描述:查询指定房间的有效的许可证<br>
    * @param houseId
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-30<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public RenovateRequisitionVO getRequisitionOutline(Long houseId) {
		RenovateLicenceDO renovateLicence= renovateLicenceDao.getRequisitionLicenceOutline(houseId);
		checkParameterIsNull(renovateLicence, "renovate:renovate_house_not_exists_licence");
		RenovateRequisitionVO renovate=assembleRequisitionD2V(renovateLicence.getRenovateRequisition());
		renovate.setRenovateLicenceNO(renovateLicence.getLicenceNumber());
		renovate.setRenovateLicenceId(renovateLicence.getKeyword());
		return renovate;
	}

   /**
    * 功能描述:<br>
    * @param requisitionDO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-23<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private RenovateRequisitionVO assembleRequisitionD2V(RenovateRequisitionDO requisitionDO){
	   RenovateRequisitionVO renovateRequisitionVO=new RenovateRequisitionVO();
	   BeanUtil.convertVoAsDo(requisitionDO, renovateRequisitionVO);
	   renovateRequisitionVO.setHouseId(requisitionDO.getKeyword());
	   renovateRequisitionVO.setHouseNumber(requisitionDO.getHouse().getHouseNumber());
	   renovateRequisitionVO.setBuildingName(requisitionDO.getHouse().getBuilding().getName());
	   renovateRequisitionVO.setRequisitionState(requisitionDO.getRequisitionState());
	   SysrefcodeDO renovateState=sysrefcodeDao.getSysrefCodesByCode(
			   SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_CODE, requisitionDO.getRequisitionState());
	   renovateRequisitionVO.setRequisitionStateView(renovateState==null ? null : renovateState.getName());

	   SysrefcodeDO level = sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.RM_COMPANY_LEVEL_CODE,requisitionDO.getLevel());
	   renovateRequisitionVO.setLevelView(level == null ? null : level.getName());

	   renovateRequisitionVO.setCreateDate(requisitionDO.getSysInfo().getSysDate());
	   HmEmployeeDomain employeeDomain= organizationalStructureService.getHmEmployeeDomainById(requisitionDO.getSysInfo().getCreator());
	   renovateRequisitionVO.setCreatorName(employeeDomain.getEmployeeName());
	   renovateRequisitionVO.setRenovateChargeItemList(assembleRenovateChargeItemList(requisitionDO.getRenovateChargeItemDOSet()));
	   return renovateRequisitionVO;
   }

	/**
	 * 功能描述:收费项目列表的装配<br>
	 * @param requisitionDO
	 * @param renovateRequisitionVO<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<RenovateChargeItemVO> assembleRenovateChargeItemList(Set<RenovateChargeItemDO> itemSet) {
		List<RenovateChargeItemVO> renovateChargeItemList=new ArrayList<RenovateChargeItemVO>();
		for(RenovateChargeItemDO item : itemSet){
			renovateChargeItemList.add(assembleRenovateChargeItem(item));
		}
		return renovateChargeItemList;
	}

	/**
	 * 功能描述:收费项目的装配<br>
	 * @param item
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2009-1-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private RenovateChargeItemVO assembleRenovateChargeItem(RenovateChargeItemDO item) {
		SysrefcodeDO feeState= sysrefcodeDao.getSysrefCodesByCode(
				   SysrefcodeDO.RM_RENOVATE_FEE_STATE_CODE, item.getFeeState());
		   SysrefcodeDO chargeObject= sysrefcodeDao.getSysrefCodesByCode(
				   SysrefcodeDO.RM_RENOVATE_CHARGE_CODE, item.getItemConfig().getChargeObject());
		   RenovateChargeItemVO itemVo=new RenovateChargeItemVO();
		   itemVo.setChargeItem(item.getItemConfig().getItemName());
		   itemVo.setFeeState(item.getFeeState());
		   itemVo.setChargeObject(item.getItemConfig().getChargeObject());
		   itemVo.setChargeObjectView(chargeObject.getName());
		   itemVo.setFeeStateView(feeState==null ? null : feeState.getName());
		   itemVo.setKeyword(item.getKeyword());
		   itemVo.setRemark(item.getRemark());
		   itemVo.setSum(item.getSum());
		return itemVo;
	}


   /**
    * 功能描述:<br>
    * @param requisitionVO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-23<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private RenovateRequisitionDO assembleRequisitionV2D( RenovateRequisitionVO requisitionVO,
		   			RenovateRequisitionDO renovateRequisitionDO){
	   BeanUtil.convertVoAsDo(requisitionVO, renovateRequisitionDO);
	   return renovateRequisitionDO;
   }


   /**
    * 功能描述:是否允许修改，根据业务规则判断<br>
    * @param renovateRequistionDO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private boolean isAllowEditRequisition(RenovateRequisitionDO renovateRequistionDO){
	   if(!isRenovateNewState(renovateRequistionDO) ){
		   throw new BusinessException("renovate:renovate_requistion_edit");
	   }
	   return true;
   }

   /**
    * 功能描述:是否允许删除，根据业务规则判断<br>
    * 1)如果不允许删除,将抛出异常(只有新建且流程处于装修申办才能删除)
    * 2)如果已经缴费,将返回false,只要有一项费用缴费都将返回false
    * 3)其它返回true
    * @param renovateRequistionDO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   public boolean isAllowDeleteRequisition(Long requistionId){
	   RenovateRequisitionDO renovateRequistionDO=renovateRequisitionDao.findById(requistionId);
	   if(!isRenovateNewState(renovateRequistionDO) ){
		   throw new BusinessException("renovate:renovate_requistion_delete",BusinessException.ERROR);
	   }
	   return isRenovateFeeNo(renovateRequistionDO);
   }

   /**
    * 功能描述:是否允许删除，根据业务规则判断<br>
    * @param renovateRequistionDO
    * @return<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private boolean isAllowBlankRequisition(RenovateRequisitionDO renovateRequistionDO){
	   if(!isRenovateNewState(renovateRequistionDO)  ){
		   throw new BusinessException("renovate:renovate_requistion_blank",BusinessException.ERROR);
	   }
	   return true;
   }

	/**
	 * 功能描述:装修申请处于新建、审批流程处于装修申办将返回真<br>
	 * @param renovateRequistionDO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private boolean isRenovateNewState(RenovateRequisitionDO renovateRequistionDO) {
		return SysrefcodeDO.RM_RENOVATE_REQUISITION_STATE_NEW.equals(renovateRequistionDO.getRequisitionState())
				   && SysrefcodeDO.RM_RENOVATE_PROCESS_STATE_ADD.equals(renovateRequistionDO.getProcessState());
	}

	/**
	 * 功能描述:装修申请还未缴费或者免收将返回真<br>
	 * (如果他的项目只有一项缴过费也认为已经缴费)
	 * @param renovateRequistionDO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-12-25<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private boolean isRenovateFeeNo(RenovateRequisitionDO renovateRequistionDO){
		boolean result=false;
		for(RenovateChargeItemDO item : renovateRequistionDO.renovateChargeItemDOSet){
			result= SysrefcodeDO.RM_FEE_STATE_IN.equals(item.getFeeState());
		}
		return !result
		 || SysrefcodeDO.RM_FEE_STATE_ESC.equals(renovateRequistionDO.getFeeState());
	}

   /**
    * 功能描述:通知新增装修申请<br>
    * @param renovateRequisitionId<br>
    * Create author:罗军林<br>
    * Create on:2008-12-25<br>
    * Edit author:<br>
    * Edit on:<br>
    * Why:<br>
    */
   private void notifyAddRenovateRequisition(Long renovateRequisitionId){
	   renovateChargeService.notifyRegisterRenovateCharge(renovateRequisitionId);
   }

   /**
    * 通知修改了装修申请
    * @roseuid 49406B60000F
    */
   private void notifyEditRenovateRequisition(Long renovateRequisitionId)
   {
	   renovateChargeService.notifyRecalculateFee(renovateRequisitionId);
   }


	public void setRenovateRequisitionDao(
			IRenovateRequisitionDao renovateRequisitionDao) {
		this.renovateRequisitionDao = renovateRequisitionDao;
	}

	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	public void setProRightChangeService(
			IProRightChangeService proRightChangeService) {
		this.proRightChangeService = proRightChangeService;
	}

	public void setRenovateChargeService(
			IRenovateChargeService renovateChargeService) {
		this.renovateChargeService = renovateChargeService;
	}

	public void setRenovateLicenceDao(IRenovateLicenceDao renovateLicenceDao) {
		this.renovateLicenceDao = renovateLicenceDao;
	}

	public void setCodingGeneratorService(
			ICodingGeneratorService codingGeneratorService) {
		this.codingGeneratorService = codingGeneratorService;
	}

	public void setOrganizationalStructureService(IOrganizationalStructureService organizationalStructureService) {
		this.organizationalStructureService = organizationalStructureService;
	}






}
